﻿using System;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using System.IO;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class NewDanhMucChiTiet : Form
    {
        private int id;
        private int parentId;

        public NewDanhMucChiTiet(int parentId)
        {
            InitializeComponent();
            this.parentId = parentId;
        }

        public NewDanhMucChiTiet(int parentId, int id)
        {
            InitializeComponent();
            this.parentId = parentId;
            this.id = id;
        }
        
        private void SaveItem(object sender, EventArgs e)
        {
            if (Validate())
            {
                string message;

                var biz = new DanhMucChiTietBiz();

                var item = new DanhMucChiTiet
                {   
                    Sdvbq = textSdvbq.Text,                    
                    Tptl = textTptl.Text,
                    Kh = textKh.Text,
                    Trang = Convert.ToInt32(numberTrang.Value),
                    Anh = Convert.ToInt32(numberAnh.Value),
                    Hinh = Convert.ToInt32(numberHinh.Value),
                    Ban = Convert.ToInt32(numberBan.Value),
                    Manh = Convert.ToInt32(numberManh.Value),
                    Can = textCan.Text,                    
                    PhanLoai = textPhanLoai.Text
                };

                if (ddlSbq.SelectedItem != null)
                {
                    var sbqId = Convert.ToInt32(ddlSbq.SelectedItem.Value);
                    item.Sbq = sbqId;
                }

                if (id > 0)
                {
                    item.Id = id;
                    biz.UpdateItem(item);

                    message = Constants.Messages.UpdateCompleted;
                }
                else
                {
                    biz.SaveItem(item);
                    message = Constants.Messages.InsertCompleted;
                }

                MessageBox.Show(message);
                Utilities.CloseForm(this, new ViewDanhMucChiTiet(parentId));
            }
        }
        
        private void Cancel(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new ViewDanhMucChiTiet(parentId));
        }

        private new bool Validate()
        {
            bool hasError = true;
            bool isFocused = false;
            errorProvider.Clear();

            if (ddlSbq.SelectedItem == null || string.IsNullOrEmpty(ddlSbq.SelectedItem.Text))
            {
                errorProvider.SetError(ddlSbq, Constants.Messages.RequireMessage);
                hasError = false;

                ddlSbq.Focus();
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textSdvbq.Text))
            {
                errorProvider.SetError(textSdvbq, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    textSdvbq.Focus();
                }
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textTptl.Text))
            {
                errorProvider.SetError(textTptl, Constants.Messages.RequireMessage);
                hasError = false;
                if (!isFocused)
                {
                    textTptl.Focus();
                }
            }

            return hasError;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.DanhMucChiTiet;
            }

            if (id > 0)
            {
                var biz = new DanhMucChiTietBiz();
                var item = biz.LoadItem(id);

                textSdvbq.Text = item.Sdvbq;
                textTptl.Text = item.Tptl;
                textKh.Text = item.Kh;
                numberTrang.Value = item.Trang;
                numberAnh.Value = item.Anh;
                numberHinh.Value = item.Hinh;
                numberBan.Value = item.Ban;
                numberManh.Value = item.Manh;
                textCan.Text = item.Can;
                textPhanLoai.Text = item.PhanLoai;
                LoadSbq(item.Sbq);
            }
            else
            {   
                LoadSbq(parentId);
            }
            ddlSbq.Focus();
        }

        private void LoadSbq(int id)
        {
            ddlSbq.Items.Clear();

            var biz = new DanhMucBiz();
            var items = biz.ViewAll();

            foreach (var item in items)
            {
                var newItem = new RadListDataItem(item.Sqb, item.Id);
                if (item.Id == id)
                {
                    newItem.Selected = true;
                    textKhlt.Text = item.Khlt;
                }
                ddlSbq.Items.Add(newItem);
            }
        }

        private void SelectSbq(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            var selected = (RadListDataItem)ddlSbq.SelectedItem;

            var id = Convert.ToInt32(selected.Value);

            var biz = new DanhMucBiz();
            var item = biz.LoadItem(id);
            textKhlt.Text = item.Khlt;
        }

        private void NewDanhMuc(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewDanhMuc());
        }
    }
}
